package com.stx.jeepay.payment.channel.plspay;

import com.jeequan.jeepay.Jeepay;
import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.response.JeepayResponse;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.model.params.plspay.PlspayConfig;
import com.stx.jeepay.core.model.params.plspay.PlspayNormalMchParams;
import com.stx.jeepay.core.utils.SpringBeansUtil;
import com.stx.jeepay.payment.model.MchAppConfigContext;
import com.stx.jeepay.payment.service.ConfigContextQueryService;
import org.apache.commons.lang3.StringUtils;

/**
 * 工具类
 */
public class PlspayKit {

    public static PayOrderCreateResponse payRequest(PayOrderEntity payOrder, MchAppConfigContext mchAppConfigContext, PayOrderCreateReqModel model) throws JeepayException {
        // 发起统一下单
        PayOrderCreateResponse response = new PayOrderCreateResponse();
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        PlspayNormalMchParams normalMchParams =
                (PlspayNormalMchParams) configContextQueryService.queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.PLSPAY);
        // 构建请求数据
        PayOrderCreateRequest request = new PayOrderCreateRequest();
        model.setMchNo(normalMchParams.getMerchantNo());
        model.setAppId(normalMchParams.getAppId());
        model.setMchOrderNo(payOrder.getPayOrderId());
        model.setAmount(payOrder.getAmount());
        model.setCurrency(payOrder.getCurrency());
        model.setClientIp(payOrder.getClientIp());
        model.setSubject(payOrder.getSubject());
        model.setBody(payOrder.getBody());
        request.setBizModel(model);

        if (normalMchParams.getSignType().equals(PlspayConfig.DEFAULT_SIGN_TYPE) || StringUtils.isEmpty(normalMchParams.getSignType())) {
            JeepayClient jeepayClient = JeepayClient.getInstance(normalMchParams.getAppId(), normalMchParams.getAppSecret(), Jeepay.getApiBase());
            response = jeepayClient.execute(request);
        } else if (normalMchParams.getSignType().equals(PlspayConfig.SIGN_TYPE_RSA2)) {
            JeepayClient jeepayClient = JeepayClient.getInstance(normalMchParams.getAppId(), normalMchParams.getRsa2AppPrivateKey(), Jeepay.getApiBase());
            response = jeepayClient.execute(request);
        }
        return response;
    }

    public static Boolean checkPayResp(JeepayResponse response, MchAppConfigContext mchAppConfigContext) {
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        PlspayNormalMchParams normalMchParams =
                (PlspayNormalMchParams) configContextQueryService.queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.PLSPAY);
        boolean isSuccess = false;
        if (normalMchParams.getSignType().equals(PlspayConfig.DEFAULT_SIGN_TYPE) || StringUtils.isEmpty(normalMchParams.getSignType())) {
            isSuccess = response.isSuccess(normalMchParams.getAppSecret());
        }else if (normalMchParams.getSignType().equals(PlspayConfig.SIGN_TYPE_RSA2)){
            isSuccess = response.isSuccessByRsa2(normalMchParams.getRsa2PayPublicKey());
        }
        return isSuccess;
    }
}
